package com.jy.adscm.wechat.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.wechat.bean.WechatUser;
import com.jy.adscm.wechat.service.WechatUserService;
import com.jy.adscm.wechat.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-09-23
 */
@RestController
@RequestMapping("adscm/wechat/wechatUser")
public class WechatUserController {
     @Autowired
    WechatUserService wechatUserService;
     @Autowired
     private RedisOperator redis;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody WechatUser wechatUser, HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        wechatUser.setOpenid(openid);
        wechatUser.setCreateTime(new Date());
        Boolean result=  wechatUserService.insertWechatUser(wechatUser);
        return  new SimpleCoreResponse("200","新增成功",result);
    }
    @RequestMapping("selectBySessionKey")
    public SimpleCoreResponse selectBySessionKey(HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        WechatUser wechatUser  =  wechatUserService.selectByOpenId(openid);
        return  new SimpleCoreResponse("200","查询成功",wechatUser);
    }
    @RequestMapping("selectByRoleId")
    public SimpleCoreResponse selectByRoleId(String pid,String roleId ,HttpServletRequest request)throws AdscmSystemException {
        String sessionKey = request.getParameter("sessionKey");
        String openid = redis.get(sessionKey);
        List<WechatUser> wechatUsers  =  wechatUserService.selectByRoleId(roleId,pid);
        return  new SimpleCoreResponse("200","查询成功",wechatUsers);
    }


}

